package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.BidReturnQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BorrowController {
        @Autowired
        private IAccountService accountService;
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private IBidRequestService bidRequestService;
        @Autowired
        private IRealAuthService realAuthService;
        @Autowired
        private IPaymentScheduleService paymentScheduleService;

        @RequestMapping("borrow")
        public String list(Model model){
                LoginInfo loginInfo = UserContext.getLoginInfo();
                if(loginInfo!=null){
                        Account account = accountService.select(loginInfo.getId());
                        UserInfo userInfo = userInfoService.select(loginInfo.getId());
                        model.addAttribute("account",account);
                        model.addAttribute("userInfo",userInfo);
                        return "borrow";
                }
                return "redirect:/borrow.html";
        }

        @RequestMapping("borrowInfo")
        public String borrowInfo(Model model){
                model.addAttribute("minBidRequestAmount", Constants.BORROW_MIN_AMOUNT);
                model.addAttribute("minBidAmount",Constants.BID_MIN_AMOUNT);
                model.addAttribute("borrowMinRate",Constants.BORROW_MIN_RATE);
                model.addAttribute("borrowMaxRate",Constants.BORROW_MAX_RATE);
                Account account = accountService.getByid(UserContext.getLoginInfo().getId());
                model.addAttribute("account",account);
                return "borrow_apply";
        }

        @RequestMapping("borrow_apply")
        @ResponseBody
        @IsLoginAnno
        public JSONResult borrowApply(BidRequest bidRequest){
                JSONResult jsonResult = new JSONResult();
                try {
                        bidRequestService.borrowApply(bidRequest);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        /**
         * 借款详情页面
         */
        @RequestMapping("borrow_info")
        public String bidRequestInfo(Long id,Model model){
                BidRequest bidRequest = bidRequestService.getById(id);
                model.addAttribute("bidRequest",bidRequest);
                LoginInfo loginInfo = UserContext.getLoginInfo();
                LoginInfo createUser = bidRequest.getCreateUser();
                //借款人的个人信息
                UserInfo borrower = userInfoService.getById(createUser.getId());
                model.addAttribute("userInfo",borrower);
                //借款人的认证信息
                RealAuth realAuth = realAuthService.getById(borrower.getRealAuthId());
                model.addAttribute("realAuth",realAuth);
                if(loginInfo!=null){ //有登录的情况下
                        //如果是借款人自己在看
                        if(loginInfo.getId().longValue()==createUser.getId().longValue()){
                                model.addAttribute("self",true);
                        }else{
                                Account account = accountService.getByid(loginInfo.getId());
                                model.addAttribute("account",account);
                        }
                }
                return "borrow_info";
        }

        @RequestMapping("borrowBidReturn_list")
        public String borrowBidReturnList(@ModelAttribute("qo")BidReturnQuery qo, Model model){
                Long id = UserContext.getLoginInfo().getId();
                qo.setCurrenBorrowerId(id);
                Account account = accountService.getByid(id);
                model.addAttribute("account",account);
                PageResult pageResult = paymentScheduleService.selectForList(qo);
                model.addAttribute("pageResult",pageResult);
                return "returnmoney_list";
        }

        @RequestMapping("returnMoney")
        @ResponseBody
        public JSONResult returnMoney(Long id){
                JSONResult jsonResult = new JSONResult();
                try {
                        bidRequestService.returnMoner(id);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

}
